﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Interactivity;
using System.Windows.Media;

namespace nRoute.Samples.SimpleMVVM.Behaviors
{
    public class SetPeersOpacityAction
        : TriggerAction<UIElement>
    {
        public SetPeersOpacityAction()
        {
            this.Opacity = 1d;
        }

        public double Opacity { get; set; }

        protected override void Invoke(object parameter)
        {
            if (this.AssociatedObject == null) return;

            var _parent = VisualTreeHelper.GetParent(VisualTreeHelper.GetParent(this.AssociatedObject)) as Panel;
            if (_parent == null) return;

            foreach (UIElement _child in _parent.Children)
            {
                _child.Opacity = this.Opacity;
            }

            var _container = VisualTreeHelper.GetParent(this.AssociatedObject) as UIElement;
            if (_container != null) _container.Opacity = 1d;
        }
    }
}
